package com.example.demo.mapper;

import com.example.demo.entity.Admin;
import com.example.demo.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class AdminMapper {
//    用户登录
    public List<Admin> AdminRegister(Admin admin) throws Exception{
        List<Admin> adminlist=new ArrayList<Admin>();
        Admin newadmin=null;
        Connection con = DBUtil.getConnection();
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("select * from Admin where admin = "+admin.getAdmin()+" and " +
                "admin_psw = "+admin.getAdmin_psw());

        while (rs.next()){
            newadmin=new Admin();
            newadmin.setAdmin_id(rs.getInt("Admin_id"));
            newadmin.setAdmin(rs.getString("admin"));
            newadmin.setAdmin_psw(rs.getString("admin_psw"));
            adminlist.add(newadmin);
        }
        return adminlist;
    }

//    用户修改密码
    public void AdminUpdate_pws(String admin,String admin_psw,String newadmin_psw) throws Exception{

        Connection con=DBUtil.getConnection();
        String sql="update Admin" +
                "set admin_psw = ?" +
                "where admin = ? and admin_psw =?";
        PreparedStatement psmt=con.prepareStatement(sql);
        psmt.setString(1,newadmin_psw);
        psmt.setString(2,admin);
        psmt.setString(3,admin_psw);
        psmt.execute();
    }

//    用户注销账号
    public void Admindelete(String admin,String admin_psw) throws Exception{

        Connection con=DBUtil.getConnection();
        String sql="" +
                "DELETE FROM Admin "+
                "WHERE admin = ?  and admin_psw = ?";
        PreparedStatement psmt=con.prepareStatement(sql);
        psmt.setString(1,admin);
        psmt.setString(2,admin_psw);
        psmt.execute();
    }

//    用户添加
public void Admindeletadd(String admin,String admin_psw) throws Exception{
    Connection con = DBUtil.getConnection();
    String sql="insert into Admin"
            + "(admin,admin_psw)"
            + "values("
            + "?,?)";
    PreparedStatement psmt = con.prepareStatement(sql);
    psmt.setString(1, admin);
    psmt.setString(2,admin_psw);
    psmt.execute();
  }
}
